#@laybase() 
#define main()

#@orderProgress()
<div style="margin-top: 15px;overflow: hidden;">
	<div class="col-md-3">
	#setLocal(tabs=["-","运营商","产品","日期","姓名","进度"])
	#setLocal(attrs=["owner","Operator","product","date","name","step"])
	#@dataTable(tabs,attrs,orderList,"/admin/photoReview")
	</div>
	#@datashowbox()
	<div class="col-md-6">
		<div  style="height: 692px;overflow: hidden;position: relative;display: none;" id="databox">
			<div id="databar" class="barbox">
				<div class="bar"></div>
			</div>
			<div id="datalist"></div>
		</div>
	</div>
	<div class="col-md-3 scrollbox" style="display: none;">
		<select id="selectphoto" class="form-control"></select>
		<div id="pageContent">
			
		</div>
	</div>
</div>

<script>
	$(function(){
		var ulnav = $("#nav-pills"),
		dataDom = $("#datalist"),
		//滚动条
		boxDom = $('#databox'),
		barbox = $('#databar'),
		datashowbox = $("#datashowbox"),//数据统计
		databox = $("#databox"),//数据展示层
		$select = $("#selectphoto");
	
		//选择下拉列表更换图片
		$select.on("change",function(){
			createImg($(this).val());
		});	
		
		//初始化图片放大
		function createImg(src){
			var image = new Image()
			image.src = src
			image.onload = function(){
				var dom = $("<div></div>")
				dom.css({
					width: $("#pageContent").parent().width(),
					height: 450
				})
				dom.iviewer({
	                src: this.src
	            });
				$("#pageContent").html(dom)
			}
			image.onerror = function(){
				$("#pageContent").html("")
				_x.show_alert_sh("找不到图片！",false)
			}
		}
		
		function getJson(type,orderId){
			var params = {
				params: {
					type:type,
					orderId:orderId
				}
			}
			return axios.get("/admin/getJson",params)
		}
		
		/*datashowbox.find("select").change(function(){
			var orderId = $(this).val();
			axios.all([getJson(5,orderId),getJson(7,orderId)])
			.then(axios.spread(function(json1,json2){
				datashowbox.hide();
		    	databox.show();
		    	console.log(json1.data)
		    	getBaseTab(json1.data)
		    	dataDom.css("top",0);
		    	_x.scrollBar.init({
					boxDom:boxDom,
					barbox:barbox,
					content:dataDom
				})
			}))
		})*/	
			
		$("#rtable").on("click","tbody tr",function(){
			if($(this).hasClass("sel"))return;
			var orderId = $(this).data("orderid");
			$(this).addClass("sel").siblings("tr").removeClass("sel");
			$("#meansp,#err,#ok").hide();
			
			axios.all([getJson(5,orderId),getJson(7,orderId)])
			.then(axios.spread(function(json1,json2){
				datashowbox.hide().siblings(".scrollbox").show();
		    	databox.show();
		    	console.log(json1.data)
		    	getBaseTab(json1.data)
		    	dataDom.css("top",0);
		    	_x.scrollBar.init({
					boxDom:boxDom,
					barbox:barbox,
					content:dataDom
				})
				
				_com.addMsg(databox,json2.data)
				
				axios.get("/admin/getAllStates")
				.then(function(progress){
					_com.renderOrderProgress(progress.data)
				})
			}))
			
		})
		
		function getBaseTab(data){
			dataDom.html("");
			//添加照片
			var photosel = $("#selectphoto"),
				options = "",
				photoLen = data.photo.length;
			if(photoLen > 0){
				createImg(data.photo[0].src);
				$("#pageContent").css("height",450);
			}else{
				$("#pageContent").css("height",0);
			}
			for(var i = 0;i < photoLen;i++){
				options += "<option value='"+ data.photo[i].src +"'>"+ data.photo[i].exp +"</option>";
			}
			photosel.html(options);
			
			//添加表单
			for(var i = 0; i < data.tabList.length; i++){
				var content = data.tabList[i].content,
					tab = data.tabList[i].tab,
					tabDom = $("<h4 data-toggle='collapse' href='#"+ content +"'>"+ tab +"</h4>"),
					contentDom = $("<div id='"+ content +"' class='collapse'></div>"),
					formbox = $("<div style='overflow: hidden;'></div>");
				dataDom.append(tabDom).append(contentDom);
				contentDom.append(formbox);
				for(var j = 0;j < data.dataList[content].length;j++){
					var that = data.dataList[content][j],
						html = "",
						iputType = that.iputType,
						val = that.value,
						paramterName = "bargainLoan." + that.paramterName,
						title = that.zhExpl,
						unit = that.unit,
						unitHtml = "",
						len = that.length,
						selectValue = that.selectValue;
					if(_x.isEmpty(len)){
						len = "";
					}else{
						len = "data-len='"+ len +"'";
					}
					if(_x.isEmpty(val)){
						val = "";
					}
					if(!_x.isEmpty(unit)){
						unitHtml = "<div class='input-group-addon'>"+ unit +"</div>";
					}
					if(_x.isEmpty(selectValue)){
						var input = "<input type='text' class='form-control' id='"+ paramterName +"' name='"+ paramterName +"' value='"+ val +"' "+ len +">";
						if(iputType == "number"){
							input = "<input type='text' class='number form-control' id='"+ paramterName +"' name='"+ paramterName +"' value='"+ val +"' "+ len +">";
						}else if(iputType == "date"){
							input = "<input type='text' class='date yz form-control' id='"+ paramterName +"' name='"+ paramterName +"' readonly='true' value='"+ val +"' "+ len +" >";
						}
						html = "<div class='form-group col-md-6'>"+
							   "	<div class='input-group'>"+
							   "		<label class='input-group-addon' for='"+ paramterName +"'>"+ title +"</label>"+
							   			input+
							   			unitHtml+
							   "	</div>"+
							   "</div>";
					}else{
						var selHtml = "<select name='"+ paramterName +"' id='"+ paramterName +"' class='form-control'>",
							selData = selectValue.split(",");
						for(var k = 0;k < selData.length;k++){
							if(val == selData[k]){
								selHtml += "<option value='"+ val +"' selected='selected'>"+ val +"</option>";
							}else{
								selHtml += "<option value='"+ selData[k] +"'>"+ selData[k] +"</option>";
							}
						}
						selHtml += "</select>";
						html = "<div class='form-group col-md-6'>"+
							   "	<div class='input-group'>"+
							   "		<label class='input-group-addon' for='"+ paramterName +"'>"+ title +"</label>"+
							   		selHtml+
							   		unitHtml+
							   "	</div>"+
							   "</div>";
					}
					formbox.append(html);
				}
				contentDom.append("<button type='button' class='btn btn-primary' style='padding: 8px 30px;' id='save_"+ content +"' data-loading-text='请稍后...'>保存</button>");
				contentDom.on('click','#save_' + content,save);
			}
			contentDom.append("<button type='button' class='btn btn-primary' style='padding: 8px 30px;margin-left:30px;' id='createTable' data-loading-text='请稍后...'>生成报表</button>");
			contentDom.append("<button type='button' class='btn btn-primary' style='padding: 8px 30px;margin-left:30px;' id='downLoad' data-loading-text='请稍后...'>下载报表</button>");
			contentDom.on('click','#createTable',function(){
				var $btn = $(this).button('loading')
				axios.get("/admin/exportbargain/carEval",{
					params: {
						"车辆评估报告.xls":"sure"
					}
				})
				.then(function(res){
					if (res.data.successList.length > 0) {
						_x.show_alert_sh("报告已生成！",true)
					}else{
						_x.show_alert_sh("生成报告失败！",false)
					}
					$btn.button('reset')
				})
				.catch(function(){
					_x.show_alert_sh("生成报告失败,请稍后重试！",false)
					$btn.button('reset')
				})
			});
			contentDom.on('click','#downLoad',function(){
				var $btn = $(this).button('loading')
				axios.get("/admin/downLoad",{
					params: {
						"车辆评估报告.xls":"sure"
					}
				})
				.then(function(res){
					if(res.data.code&&data.code==0){
						_x.show_alert_sh(res.data.msg,false)
					}else{
						location.href = res.data.msg;
						_x.show_alert_sh("下载成功！",true)
					}
					$btn.button('reset')
				})
				.catch(function(){
					_x.show_alert_sh("生成失败，请稍后重试！",false)
					$btn.button('reset')
				})
			});
		}
		
		
		//点击保存
		function save(){
			var $btn = $(this),
				yzDoms = $btn.parent().find(".yz"),
				mark = yanzheng(yzDoms);
			if(mark){
				$btn.button('loading')
				var data = {};
				yzDoms.each(function(){
					var that = $(this);
					data[that.attr("name")] = _x.trimAll(that.val());
				})
				
				axios.post("/admin/fixBargainInfo",_x.qsStringify(data))
				.then(function(res){
					if(res.data.code == 1){
						_x.show_alert_sh(res.data.msg,true)
					}else{
						_x.show_alert_sh(res.data.msg,false)
					}
					$btn.button('reset')
				})
				.catch(function(){
					$btn.button('reset')
					_x.show_alert_sh("您的网络异常，请稍后重试！",false)
				})
			}
		}
		
		//表单验证
		function yanzheng(yzDoms,mark){
			var mark = true,
				errTip = []
			yzDoms.each(function(){
				var that = $(this),
					val = _x.trimAll(that.val()),
					labelText = that.siblings("label").text()
				
				if(_x.isEmpty(val)){//空判断
					mark = false;
					that.attr("placeholder","此项不能为空！");
					that.parents(".form-group").addClass("has-error");
					errTip.push(labelText + "不能为空！")
				}else if(!_x.isEmpty(that.data("len")) && val.length != that.data("len")){
					mark = false;
					that.attr("placeholder","长度必须为"+ that.data("len") + "位");
					that.parents(".form-group").addClass("has-error");
					errTip.push(labelText + "长度必须为"+ that.data("len") + "位")
				}else if(that.hasClass("number") && isNaN(+val)){
					mark = false;
					that.attr("placeholder","请输入数字！");
					that.parents(".form-group").addClass("has-error");
					errTip.push(labelText + "只能输入数字！")
				}else{
					that.parents(".form-group").removeClass("has-error");
				}
			})
			if(errTip.length > 0)_x.show_alert_sh(errTip[0],false)
			return mark;
		}
		
		//修改过的才需要验证
		dataDom.on("change",".form-control",function(){
			$(this).addClass("yz");
		})
		
		//失去焦点验证
		dataDom.on("blur",".yz",function(){
			yanzheng($(this));
		})
		
		//绑定日历
		dataDom.on("click",".date",laydate);
		
		//滚动条
		_x.scrollBar.init({
			boxDom:boxDom,
			barbox:barbox,
			content:dataDom
		})
		
		dataDom.on("shown.bs.collapse",".collapse", function () {
			_x.scrollBar.init({
				boxDom:boxDom,
				barbox:barbox,
				content:dataDom
			})
			var _top = $(this)[0].offsetTop,
				dd = $(this).prev("h4").outerHeight() + 15;
			if(dataDom.height() - _top <= boxDom.outerHeight() && dataDom.height() > boxDom.outerHeight()){
				_top = (dataDom.height() - boxDom.height() + dd+15)
				dataDom.animate({
					"top":-_top + dd
				},function(){
					var maxHeight = barbox.height() - barbox.children(".bar").height(),
						bit = Math.abs(dataDom[0].offsetTop) / dataDom.height(),
						$top = bit * barbox.height();
					if($top <= 0)$top = 0;
					if($top >= maxHeight)$top = maxHeight;
					barbox.children(".bar").css({"top":$top})
				})
			}
		})
		
		dataDom.on("hidden.bs.collapse",".collapse", function () {
			_x.scrollBar.init({
				boxDom:boxDom,
				barbox:barbox,
				content:dataDom
			})
			var _top = $(this).parent()[0].offsetTop,
				dd = $(this).prev("h4").outerHeight() / 2;
			if(dataDom.height() - Math.abs(_top) <= boxDom.outerHeight() && dataDom.height() > boxDom.outerHeight()){
				_top = (Math.abs(_top) + dd) - (boxDom.outerHeight() - (dataDom.height() - Math.abs(_top)));
				dataDom.animate({
					"top":-_top 
				},function(){
					var maxHeight = barbox.height() - barbox.children(".bar").height(),
						bit = Math.abs(dataDom[0].offsetTop) / dataDom.height(),
						$top = bit * barbox.height();
					if($top <= 0)$top = 0;
					if($top >= maxHeight)$top = maxHeight;
					barbox.children(".bar").css({"top":$top})
				})
			}else{
				dataDom.animate({
					"top":0
				})
			}
		})
	})
</script>
#end
